package com.flyco.modules.stm.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyco.modules.fms.vo.FmsReportVO;
import com.flyco.modules.home.vo.OrderTotalPayAmountVO;
import com.flyco.modules.oms.entity.OmsOrder;
import com.flyco.modules.oms.entity.OmsOrderInvoice;
import com.flyco.modules.oms.entity.OmsOrderItem;
import com.flyco.modules.stm.mapper.StmOrderInvoiceMapper;
import com.flyco.modules.stm.mapper.StmOrderItemMapper;
import com.flyco.modules.stm.mapper.StmOrderMapper;
import com.flyco.modules.stm.service.IStmOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;


/**
 * @Description: 订单管理
 * @Author: flyco
 * @Date:   2019-12-10
 * @Version: V1.0
 */
@DS("store")
@Service
public class StmOrderServiceImpl extends ServiceImpl<StmOrderMapper, OmsOrder> implements IStmOrderService {

    @Autowired
    private StmOrderMapper stmOrderMapper;
    @Autowired
    private StmOrderItemMapper stmOrderItemMapper;
    @Autowired
    private StmOrderInvoiceMapper stmOrderInvoiceMapper;

    @Override
    public Map<String, Object> indexOrderInit() {
        return stmOrderMapper.indexOrderInit();
    }

    @Override
    public List<Map<String,Object>> indexOrderBar() {
        return stmOrderMapper.indexOrderBar();
    }

    @Override
    public List<Map<String,Object>> indexOrderMiniArea() {
        return stmOrderMapper.indexOrderMiniArea();
    }

    @Override
    public List<Map<String,Object>> indexOrderRank() {
        return stmOrderMapper.indexOrderRank();
    }

    @Override
    public List<OrderTotalPayAmountVO> indexOrderTotalPayAmount(Date dayStart, Date dayEnd) {
        return stmOrderMapper.indexOrderTotalPayAmount(dayStart, dayEnd);
    }

    @Override
    public Page<OmsOrder> getOrderPage(Page<OmsOrder> page, Wrapper<OmsOrder> wrapper, String productSn) {
        return page.setRecords(stmOrderMapper.getOrderPage(page, wrapper, productSn));
    }

    @Override
    public List<FmsReportVO> getFmsReport(Date dayStart, Date dayEnd) {
        return stmOrderMapper.getFmsReport(dayStart, dayEnd);
    }

    @Override
    @Transactional
    public void saveMain(OmsOrder omsOrder, List<OmsOrderItem> omsOrderItemList, List<OmsOrderInvoice> omsOrderInvoiceList) {
        stmOrderMapper.insert(omsOrder);
        for(OmsOrderItem entity:omsOrderItemList) {
            //外键设置
            entity.setOrderId(omsOrder.getId());
            stmOrderItemMapper.insert(entity);
        }
        for(OmsOrderInvoice entity:omsOrderInvoiceList) {
            //外键设置
            entity.setOrderId(omsOrder.getId());
            stmOrderInvoiceMapper.insert(entity);
        }
    }

}
